<?php
require_once './common.php';    // 通用文件，包括配置、Xiaonei初始化等
?>

<?php

//为邀请好友加钱。
if (isset($_GET['invite_id'])) {

  $invite_ids = $_GET['invite_id'];
  $invitermoney=2000;
  $inviteemoney=500;
  try{
		$method="getOsInfo";
		$params['invite_ids']=$invite_ids;
		$ivt=$my->api_client->invitations($method,$params);	

		if ($ivt!=NULL) {
			$connmoney = get_db_conn();
			//为邀请人加钱。
			$sqlmoney = "SELECT * FROM `".$config['db_prefix']."users` WHERE `uid` = '".$ivt[0]['inviter_uid']."' ";
			$inviter_result = mysql_query($sqlmoney, $connmoney);
			if(mysql_num_rows($inviter_result)==1) {
				$inviter_info = mysql_fetch_array($inviter_result);
				$sqlmoney = "UPDATE `".$config['db_prefix']."users` SET money = ".($inviter_info['money']+$invitermoney)." WHERE `uid` = '".$ivt[0]['inviter_uid']."'";
				mysql_query($sqlmoney, $connmoney);
			}
			//为被邀请人加钱。
			$sqlmoney = "SELECT * FROM `".$config['db_prefix']."users` WHERE `uid` = '".$ivt[0]['invitee_uid']."' ";
			$invitee_result = mysql_query($sqlmoney, $connmoney);
			if(mysql_num_rows($invitee_result)==1) {
				$invitee_info = mysql_fetch_array($invitee_result);
				$sqlmoney = "UPDATE `".$config['db_prefix']."users` SET money = ".($invitee_info['money']+$inviteemoney)." WHERE `uid` = '".$ivt[0]['invitee_uid']."'";
				mysql_query($sqlmoney, $connmoney);
			}

			try{
				$method="publishTemplatizedAction";
				$params['template_id']=3;
				$params['body_data']='{"inviter":"<yk:name uid=\''.$ivt[0]['inviter_uid'].'\' linked=\'true\' />", "invitee":"<yk:name uid=\''.$ivt[0]['invitee_uid'].'\' linked=\'true\' />", "invitermoney":"'.$invitermoney.'", "inviteemoney":"'.$inviteemoney.'", "game":"<a href=\'http://apps.guanxi.koubei.com/php_songli/\'>礼尚往来</a>"}';
				$feed=$my->api_client->feed($method,$params);	
				//print_R($feed);
			}catch(Exception $ex){		
				//exit();
			}

			$notification_info = '<yk:name uid="'.$ivt[0]['invitee_uid'].'" linked="true" /> 接受了你发出的 <a href="http://apps.guanxi.koubei.com/php_songli/">礼尚往来</a> 的邀请，你因此得到了 '.$invitermoney.' 金币! 快去 <a href="http://apps.guanxi.koubei.com/php_songli/">送礼物</a> 感谢TA！';
			try{
				$method="send";
				$params['to_ids']=$ivt[0]['inviter_uid'];
				$params['notification']="$notification_info";
				$send=$my->api_client->notifications($method,$params);	
				//print_R($send);
			}catch(Exception $ex){		
				//exit();
			}

		}
		//print_R($ivt);
  }catch(Exception $ex){		
		//exit();	
  }

}

if (isset($_POST['flower_id']) && isset($_POST['friend_id'])) {
  $from = $uid;
  $to = $_POST['friend_id'];
  $flower = intval($_POST['flower_id']);
  $message = $my->no_magic_quotes($_POST['flower_message']);
  if($from && $to) {
    if (count($message) > 140) {
      echo message_box('错误提示', '赠言不能大于140个字符。', 'error');
    } else if(do_send($from, $to, $flower, $message)) {
      echo message_box('发送成功', '您的礼物已送出。', 'success');
    } else {
      echo message_box('发送失败', '请再试一次。', 'error');
    }
  }

}

//echo message_box('发送成功', do_send($from, $to, $flower, $message), 'success');

?>

<!-- MYML Editor 内容编辑器 -->

      ﻿﻿
      <!-- 创建一组标准的 Xiaonei 标签导航栏。 -->
      <div id="sub-nav">
      <ul>
        <li style="padding-right:10px;" class="selected"><a href="<? echo $config['app_app_url'] ?>index.php">送礼给他/她</a></li>
        <li style="padding-right:10px;"><a href="<? echo $config['app_app_url'] ?>send.php">送出的礼物</a></li>
        <li style="padding-right:10px;"><a href="<? echo $config['app_app_url'] ?>receive.php">接收的礼物</a></li>
        <li style="padding-right:10px;"><a href="<? echo $config['app_app_url'] ?>invite.php">邀请好友加入</a></li>
        <li style="padding-right:10px;"><a href="<? echo $config['app_app_url'] ?>mine.php">我的账户</a></li>
      </ul>
      </div>
      <div id="app1013042_content" style="width: 95%;    margin: 20px auto;">
        <!-- MYML Editor 内容编辑器 -->
        <form id="app1013042_" action="<? echo $config['app_app_url']?>" method="post">
          <table class="editorkit" border="0" cellspacing="0" style="margin:0.7em 0;width:750px">
            <tr class="width_setter">
              <th style="width:10px"></th>
              <td></td>
            </tr>
            <tr>
              <th><label class="detached_label"></label></th>
              <td class="editorkit_row" style="padding:3px 0;"><h2 style="border-bottom: 1px solid #cccccc;">选择礼物</h2>
                <ul class="avatar_list">
                
                <?php 
                foreach ($config['flowers'] as $key => $flower) {
                  echo '<li style="float: left;    width: 72px;    padding: 5px 0;">';
                  echo '<img src="' . $config['app_url'] . 'images/flowers/' . $key . '.jpg" />';
                  echo '<p style="width: 72px;    overflow: hidden;    white-space: nowrap;"><input type="radio" value="' . $key . '" name="flower_id" class="avatar_radio" />';
                  echo '' . $flower . '</p>';
                  echo '</li>';
                }
                ?>

                </ul></td>
              <td class="right_padding"></td>
            </tr>
            <tr>
              <th><label class="detached_label"></label></th>
              <td class="editorkit_row"><h2 style="border-bottom: 1px solid #cccccc;">选择好友</h2>
              <?php if(count($friends)) { ?>
                <ul class="avatar_list">
                
                <?php
                foreach($friends as $friend) {
                  echo '<li style="float: left;    width: 72px;    padding: 5px 0;">';
                  echo '<yk:profile-pic uid="'.$friend["id"].'" size="tiny" linked="true" />';
                  echo '<p style="width: 72px;    overflow: hidden;    white-space: nowrap;"><input type="radio" value="'.$friend["id"].'" name="friend_id" />';
                  echo '<yk:name uid="'.$friend["id"].'" /></p>';
                  echo '</li>';
                }
                ?>
                  
                </ul>
              <?php } else { ?>
              您还没有好友，请先添加好友。
              <?php } ?>
              </td>
              <td class="right_padding"></td>
            </tr>
            <tr>
              <th><label class="detached_label"></label></th>
              <td class="editorkit_row"><h2 style="border-bottom: 1px solid #cccccc;">赠言</h2>
                <p style=" margin: 8px 0;">写上您的祝福的话，140个字符，每个汉字占3个字符。</p></td>
              <td class="right_padding"></td>
            </tr>
            <tr>
              <th><label class="detached_label"></label></th>
              <td class="editorkit_row"><textarea style="height:80px;padding:3px 2px; width:95%; line-height:16px; border-style:solid; border-width:1px; border-color:#7C7C7C #C3C3C3 #DDD;" name="flower_message" cols="80" rows="5" id="app1013042_"></textarea></td>
              <td class="right_padding"></td>
            </tr>
            <tr>
              <th></th>
              <td class="editorkit_buttonset"><input type="submit" class="input-button" id="app1013042_" name="" value=" 发 送 " />
              </td>
              <td class="right_padding"></td>
            </tr>
          </table>
        </form>
      </div>
